﻿Imports System.Data
Imports MyGeneration.dOOdads

Public Class rc_Movimientos

    Private _MessageError As Exception = Nothing
    Public Property MessageError() As Exception
        Get
            Return _MessageError
        End Get
        Set(value As Exception)
            _MessageError = value
        End Set
    End Property
    Private log__1 As New Class_RC_Controller.rc_Mantenimiento()
#Region "Movimientos"

    Public Function Insert_Movimiento(conn As String, codNota As Integer, codVenta As Integer, fecha As DateTime, codSeccion As Integer,
                                      movimientoTipo As String, codUsuario As Integer, codResponsable As Integer) As Integer

        Dim tx As TransactionMgr

        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim mov As New Class_RC_Data.Class_RC.MOV_PRODUCTOS(conn)

            mov.AddNew()

            If codNota > 0 Then
                mov.CodNota = codNota
            End If

            If codVenta > 0 Then
                mov.CodVenta = codVenta
            End If

            mov.Fecha = fecha
            mov.CodSeccion = codSeccion
            mov.MovimientoTipo = movimientoTipo
            mov.CodResponsable = codResponsable
            mov.CodUsuario = codUsuario
            mov.FechaActualizacion = DateTime.Now
            mov.Save()
            If movimientoTipo = "ENTRADA" Then
                log__1.Insert_Log(conn, "Inserto registro, codigo: " & mov.CodMovPedido, codUsuario, 9, mov.DefaultView.Table)
            Else
                log__1.Insert_Log(conn, "Inserto registro, codigo: " & mov.CodMovPedido, codUsuario, 10, mov.DefaultView.Table)
            End If

            tx.CommitTransaction()

            Return mov.CodMovPedido

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return 0
        End Try

    End Function

    Public Function Update_Movimiento(conn As String, codMovimiento As Integer,
                                     codNota As Integer, codVenta As Integer, fecha As DateTime, codSeccion As Integer,
                                      movimientoTipo As String, codUsuario As Integer, codResponsable As Integer) As Boolean

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim mov As New Class_RC_Data.Class_RC.MOV_PRODUCTOS(conn)
            mov.Where.CodMovPedido.Value = codMovimiento

            If mov.Query.Load Then

                If codNota > 0 Then
                    mov.CodNota = codNota
                End If

                If codVenta > 0 Then
                    mov.CodVenta = codVenta
                End If

                mov.Fecha = fecha
                mov.CodSeccion = codSeccion
                mov.MovimientoTipo = movimientoTipo
                mov.CodResponsable = codResponsable
                mov.CodUsuario = codUsuario
                mov.FechaActualizacion = DateTime.Now
                mov.Save()

            End If

            tx.CommitTransaction()
            Return True

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return False
        End Try

    End Function

    Public Function Delete_Movimiento(conn As String, codMovimiento As Integer) As Boolean

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim mov As New Class_RC_Data.Class_RC.MOV_PRODUCTOS(conn)
            mov.Where.CodMovPedido.Value = codMovimiento

            If mov.Query.Load Then

                mov.MarkAsDeleted()
                mov.Save()

            End If

            tx.CommitTransaction()
            Return True

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return False
        End Try

    End Function

    Public Function get_Movimientos_All(conn As String, codMov As Integer, fechaInicio As DateTime,
                                   fechaFin As DateTime, tipoMov As String, cliente As String,
                                   nroPedido As Integer, nroFactura As String, ruc As String) As DataTable
        Try

            Dim vMov As New Class_RC_Data.Class_RC.VistaMovProductos(conn)

            If codMov > 0 Then
                vMov.Where.CodMovPedido.Value = codMov
            End If

            If nroPedido > 0 Then
                vMov.Where.NroNota.Value = nroPedido


            Else
                vMov.Where.FechaMov.BetweenBeginValue = New DateTime(fechaInicio.Year, fechaInicio.Month, fechaInicio.Day, 0, 0, 0)
                vMov.Where.FechaMov.BetweenEndValue = New DateTime(fechaFin.Year, fechaFin.Month, fechaFin.Day, 23, 59, 59)
                vMov.Where.FechaMov.Operator = WhereParameter.Operand.Between
            End If

            If tipoMov <> "" Then
                vMov.Where.MovimientoTipo.Value = tipoMov
            End If

            If cliente <> "" Then
                vMov.Where.Cliente.Value = "%" + cliente + "%"
                vMov.Where.Cliente.Operator = WhereParameter.Operand.Like
            End If

            'If ruc <> "" Then
            '    vMov.Where.Ruc.Value = "%" + ruc + "%"
            '    vVentas.Where.Ruc.Operator = WhereParameter.Operand.Like
            'End If

            vMov.Query.AddOrderBy("fechaMov", WhereParameter.Dir.DESC)

            If vMov.Query.Load Then
                If vMov.DefaultView.Table.Rows.Count > 0 Then
                    Return vMov.DefaultView.Table
                Else
                    Return Nothing
                End If

            Else
                Return Nothing

            End If

        Catch ex As Exception
            MessageError = ex
            Return Nothing
        End Try

    End Function

    Public Function get_Movimientos_Reporte(ByVal conn As String, ByVal codMov As Integer, ByVal fechaInicio As DateTime,
                                   ByVal fechaFin As DateTime, ByVal tipoMov As String, ByVal cliente As String,
                                   ByVal nroPedido As String, ByVal nroFactura As String, ByVal ruc As String) As DataTable
        Try

            Dim vMov As New Class_RC_Data.Class_RC.VistaMovProductosImprimir(conn)

            If codMov > 0 Then
                vMov.Where.CodMovPedido.Value = codMov
            End If

            'If nroPedido > 0 Then
            '    vMov.Where.NroNota.Value = "%" & nroPedido & "%"
            '    vMov.Where.NroNota.Operator = WhereParameter.Operand.Like
            'Else
            '    vMov.Where.FechaMov.BetweenBeginValue = New DateTime(fechaInicio.Year, fechaInicio.Month, fechaInicio.Day, 0, 0, 0)
            '    vMov.Where.FechaMov.BetweenEndValue = New DateTime(fechaFin.Year, fechaFin.Month, fechaFin.Day, 23, 59, 59)
            '    vMov.Where.FechaMov.Operator = WhereParameter.Operand.Between
            'End If

            If tipoMov <> "" Then
                vMov.Where.MovimientoTipo.Value = tipoMov
            End If

            If cliente <> "" Then
                vMov.Where.Cliente.Value = "%" + cliente + "%"
                vMov.Where.Cliente.Operator = WhereParameter.Operand.Like
            End If

            'If ruc <> "" Then
            '    vMov.Where.Ruc.Value = "%" + ruc + "%"
            '    vVentas.Where.Ruc.Operator = WhereParameter.Operand.Like
            'End If

            vMov.Query.AddOrderBy("fechaMov", WhereParameter.Dir.DESC)

            If vMov.Query.Load Then
                If vMov.DefaultView.Table.Rows.Count > 0 Then
                    Return vMov.DefaultView.Table
                Else
                    Return Nothing
                End If

            Else
                Return Nothing

            End If

        Catch ex As Exception
            MessageError = ex
            Return Nothing
        End Try

    End Function

    Public Function get_Productos_Saldo_ByID(conn As String, codProducto As Integer, codDeposito As Integer, codSeccion As Integer) As DataTable
        Try

            Dim vVentas As New Class_RC_Data.Class_RC.Vista_ProductosDeposito(conn)

            vVentas.Where.CodProducto.Value = codProducto
            vVentas.Where.CodSeccion.Value = codSeccion
            vVentas.Where.CodDeposito.Value = codDeposito
            vVentas.Where.Saldo.Value = 0
            vVentas.Where.Saldo.Operator = WhereParameter.Operand.GreaterThan

            If vVentas.Query.Load Then
                If vVentas.DefaultView.Table.Rows.Count > 0 Then
                    Return vVentas.DefaultView.Table
                Else
                    Return Nothing
                End If

            Else
                Return Nothing

            End If

        Catch ex As Exception
            MessageError = ex
            Return Nothing
        End Try

    End Function

    Public Function get_Materiales_Saldo_ByID(conn As String, codMaterial As Integer, codDeposito As Integer, codSeccion As Integer) As DataTable
        Try

            Dim vVentas As New Class_RC_Data.Class_RC.Vista_MaterialesDeposito(conn)

            vVentas.Where.CodMaterial.Value = codMaterial
            vVentas.Where.CodSeccion.Value = codSeccion
            vVentas.Where.CodDeposito.Value = codDeposito
            vVentas.Where.Saldo.Value = 0
            vVentas.Where.Saldo.Operator = WhereParameter.Operand.GreaterThan

            If vVentas.Query.Load Then
                If vVentas.DefaultView.Table.Rows.Count > 0 Then
                    Return vVentas.DefaultView.Table
                Else
                    Return Nothing
                End If

            Else
                Return Nothing

            End If

        Catch ex As Exception
            MessageError = ex
            Return Nothing
        End Try

    End Function

    Public Function get_Movimientos_ByID(conn As String, codMov As Integer) As DataTable
        Try

            Dim vVentas As New Class_RC_Data.Class_RC.VistaMovProductos(conn)

            If codMov > 0 Then
                vVentas.Where.CodMovPedido.Value = codMov
            End If

            If vVentas.Query.Load Then
                If vVentas.DefaultView.Table.Rows.Count > 0 Then
                    Return vVentas.DefaultView.Table
                Else
                    Return Nothing
                End If

            Else
                Return Nothing

            End If

        Catch ex As Exception
            MessageError = ex
            Return Nothing
        End Try

    End Function

    Public Function Insert_Movimientos_Detalle(conn As String, codMovPedido As Integer, codProducto As Integer,
                                               cantidad As Decimal, tipoMovimiento As String, codSeccion As Integer,
                                               codDeposito As Integer) As Boolean

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim detalle As New Class_RC_Data.Class_RC.DETALLEMOV_PRODUCTOS(conn)

            If tipoMovimiento = "SALIDA" And codProducto > 0 Then

                Dim saldo As New Class_RC_Data.Class_RC.Vista_ProductosDeposito(conn)
                saldo.Where.CodProducto.Value = codProducto
                saldo.Where.CodSeccion.Value = codSeccion
                saldo.Where.CodDeposito.Value = codDeposito
                'saldo.Where.Saldo.Value = cantidad
                'saldo.Where.Saldo.Operator = WhereParameter.Operand.Greate
                If Not saldo.Query.Load Then
                    Throw New Exception("No posee cantidad necesaria del producto en el deposito seleccionado")
                Else

                    If Integer.Parse(saldo.DefaultView.Table.Rows(0)("Saldo")) < cantidad Then

                        Throw New Exception("No posee cantidad necesaria del producto en el deposito seleccionado")

                    End If

                End If

            End If


            Dim pro As New Class_RC_Data.Class_RC.PRODUCTOS(conn)
            pro.Where.CodProducto.Value = codProducto
            If pro.Query.Load Then

                If tipoMovimiento = "ENTRADA" Then
                    pro.CantExistente += cantidad
                ElseIf tipoMovimiento = "SALIDA" Then

                    'If (pro.CantExistente - cantidad) >= 0 Then
                    '    pro.CantExistente -= cantidad
                    'Else
                    '    Throw New Exception("No posee cantidad necesaria del producto")
                    'End If

                End If

                pro.Save()

            End If

            detalle.AddNew()
            detalle.CodMovPedido = codMovPedido
            detalle.CodProducto = codProducto
            detalle.Cantidad = cantidad
            detalle.Save()

            tx.CommitTransaction()

            Return True

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return False
        End Try

    End Function

    Public Function Delete_Movimientos_Detalle(conn As String, codDetalleMovPedido As Integer) As Boolean

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim detalle As New Class_RC_Data.Class_RC.DETALLEMOV_PRODUCTOS(conn)

            detalle.Where.CodMovPedidoDetalle.Value = codDetalleMovPedido

            If detalle.Query.Load Then
                detalle.MarkAsDeleted()
                detalle.Save()
            End If

            tx.CommitTransaction()

            Return True

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return False
        End Try

    End Function

    Public Function get_Movimientos_Detalles(conn As String, codMovPedido As Integer) As DataTable
        Try

            Dim vDetalle As New Class_RC_Data.Class_RC.VistaMovProductosDetalle(conn)

            If codMovPedido > 0 Then
                vDetalle.Where.CodMovPedido.Value = codMovPedido
            End If

            If vDetalle.Query.Load Then
                If vDetalle.DefaultView.Table.Rows.Count > 0 Then
                    Return vDetalle.DefaultView.Table
                Else
                    Return Nothing
                End If

            Else
                Return Nothing

            End If

        Catch ex As Exception
            MessageError = ex
            Return Nothing
        End Try

    End Function

    Public Function get_Depositos_All(conn As String) As DataTable
        Try

            Dim vDetalle As New Class_RC_Data.Class_RC.DEPOSITOS(conn)

            If vDetalle.Query.Load Then
                If vDetalle.DefaultView.Table.Rows.Count > 0 Then
                    Return vDetalle.DefaultView.Table
                Else
                    Return Nothing
                End If

            Else
                Return Nothing

            End If

        Catch ex As Exception
            MessageError = ex
            Return Nothing
        End Try

    End Function

    Public Function get_SeccionesDepositos_All(conn As String, codDeposito As Integer) As DataTable
        Try

            Dim vDetalle As New Class_RC_Data.Class_RC.SECCIONES(conn)
            vDetalle.Where.CodDeposito.Value = codDeposito

            If vDetalle.Query.Load Then
                If vDetalle.DefaultView.Table.Rows.Count > 0 Then
                    Return vDetalle.DefaultView.Table
                Else
                    Return Nothing
                End If

            Else
                Return Nothing

            End If

        Catch ex As Exception
            MessageError = ex
            Return Nothing
        End Try

    End Function

    Public Function get_Productos_Deposito(ByVal conn As String) As DataTable
        Try

            Dim vproductos As New Class_RC_Data.Class_RC.Vista_ProductosDeposito(conn)
            If vproductos.Query.Load Then
                If vproductos.DefaultView.Table.Rows.Count > 0 Then
                    Return vproductos.DefaultView.Table
                Else
                    Return Nothing
                End If

            Else
                Return Nothing

            End If

        Catch ex As Exception
            MessageError = ex
            Return Nothing
        End Try

    End Function
   
#End Region

#Region "MovimientosMateriales"

    Public Function Insert_MovimientoMaterial(conn As String, codNota As Integer, codCompra As Integer, fecha As DateTime, codSeccion As Integer,
                                      movimientoTipo As String, codUsuario As Integer, codResponsable As Integer) As Integer

        Dim tx As TransactionMgr

        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim mov As New Class_RC_Data.Class_RC.MOV_MATERIALES(conn)
            mov.Where.CodCompra.Value = codCompra

            If movimientoTipo = "ENTRADA" And mov.Query.Load Then
                Throw New Exception("Los materiales de esta factura ya han ingresado al Deposito")
            Else
                mov.AddNew()

                If codNota > 0 Then
                    mov.CodNota = codNota
                End If

                If codCompra > 0 Then
                    mov.CodCompra = codCompra
                End If

                mov.Fecha = fecha
                mov.CodSeccion = codSeccion
                mov.MovimientoTipo = movimientoTipo
                mov.CodResponsable = codResponsable
                mov.CodUsuario = codUsuario
                mov.FechaActualizacion = DateTime.Now
                mov.Save()
                If movimientoTipo = "ENTRADA" Then
                    log__1.Insert_Log(conn, "Inserto registro, codigo: " & mov.CodMovMaterial, codUsuario, 9, mov.DefaultView.Table)
                Else
                    log__1.Insert_Log(conn, "Inserto registro, codigo: " & mov.CodMovMaterial, codUsuario, 10, mov.DefaultView.Table)
                End If
            End If

                        tx.CommitTransaction()

            Return mov.CodMovMaterial

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return 0
        End Try

    End Function

    Public Function Update_MovimientoMaterial(conn As String, codMovimiento As Integer,
                                     codNota As Integer, codCompra As Integer, fecha As DateTime, codSeccion As Integer,
                                      movimientoTipo As String, codUsuario As Integer, codResponsable As Integer) As Boolean

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim mov As New Class_RC_Data.Class_RC.MOV_MATERIALES(conn)
            mov.Where.CodMovMaterial.Value = codMovimiento

            If mov.Query.Load Then

                If codNota > 0 Then
                    mov.CodNota = codNota
                End If

                If codCompra > 0 Then
                    mov.CodCompra = codCompra
                End If

                mov.Fecha = fecha
                mov.CodSeccion = codSeccion
                mov.MovimientoTipo = movimientoTipo
                mov.CodResponsable = codResponsable
                mov.CodUsuario = codUsuario
                mov.FechaActualizacion = DateTime.Now
                mov.Save()

            End If

            tx.CommitTransaction()
            Return True

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return False
        End Try

    End Function

    Public Function Delete_MovimientoMaterial(conn As String, codMovimiento As Integer) As Boolean

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim mov As New Class_RC_Data.Class_RC.MOV_MATERIALES(conn)
            mov.Where.CodMovMaterial.Value = codMovimiento

            If mov.Query.Load Then

                mov.MarkAsDeleted()
                mov.Save()

            End If

            tx.CommitTransaction()
            Return True

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return False
        End Try

    End Function

    Public Function get_MovimientosMaterial_All(conn As String, codMov As Integer, fechaInicio As DateTime,
                                   fechaFin As DateTime, tipoMov As String, proveedor As String, cliente As String,
                                   nroPedido As Integer, nroFactura As String, rucPro As String, rucCliente As String) As DataTable
        Try

            Dim vMov As New Class_RC_Data.Class_RC.VistaMovMateriales(conn)

            If codMov > 0 Then
                vMov.Where.CodMovMaterial.Value = codMov
            End If

            If nroPedido > 0 Then
                vMov.Where.NroNota.Value = nroPedido
            Else
                vMov.Where.FechaMov.BetweenBeginValue = New DateTime(fechaInicio.Year, fechaInicio.Month, fechaInicio.Day, 0, 0, 0)
                vMov.Where.FechaMov.BetweenEndValue = New DateTime(fechaFin.Year, fechaFin.Month, fechaFin.Day, 23, 59, 59)
                vMov.Where.FechaMov.Operator = WhereParameter.Operand.Between
            End If

            If tipoMov <> "" Then
                vMov.Where.MovimientoTipo.Value = tipoMov
            End If

            If proveedor <> "" Then
                vMov.Where.Proveedor.Value = "%" + proveedor + "%"
                vMov.Where.Proveedor.Operator = WhereParameter.Operand.Like
            End If

            If cliente <> "" Then
                vMov.Where.Cliente.Value = "%" + cliente + "%"
                vMov.Where.Cliente.Operator = WhereParameter.Operand.Like
            End If

            If rucPro <> "" Then
                vMov.Where.Ruc.Value = "%" + rucPro + "%"
                vMov.Where.Ruc.Operator = WhereParameter.Operand.Like
            End If

            If rucCliente <> "" Then
                vMov.Where.RucCliente.Value = "%" + rucCliente + "%"
                vMov.Where.RucCliente.Operator = WhereParameter.Operand.Like
            End If

            vMov.Query.AddOrderBy("fechaMov", WhereParameter.Dir.DESC)

            If vMov.Query.Load Then
                If vMov.DefaultView.Table.Rows.Count > 0 Then
                    Return vMov.DefaultView.Table
                Else
                    Return Nothing
                End If

            Else
                Return Nothing

            End If

        Catch ex As Exception
            MessageError = ex
            Return Nothing
        End Try

    End Function

    Public Function get_MovimientosMaterial_ByID(conn As String, codMov As Integer) As DataTable
        Try

            Dim vVentas As New Class_RC_Data.Class_RC.VistaMovMateriales(conn)

            If codMov > 0 Then
                vVentas.Where.CodMovMaterial.Value = codMov
            End If

            If vVentas.Query.Load Then
                If vVentas.DefaultView.Table.Rows.Count > 0 Then
                    Return vVentas.DefaultView.Table
                Else
                    Return Nothing
                End If

            Else
                Return Nothing

            End If

        Catch ex As Exception
            MessageError = ex
            Return Nothing
        End Try

    End Function

    Public Function Insert_MovimientosMaterial_Detalle(conn As String, codMovMaterial As Integer, codMaterial As Integer,
                                                       cantidad As Decimal, tipoMovimiento As String, codSeccion As Integer,
                                               codDeposito As Integer) As Boolean

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim detalle As New Class_RC_Data.Class_RC.DETALLEMOV_MATERIALES(conn)

            If tipoMovimiento = "SALIDA" And codMaterial > 0 Then

                Dim saldo As New Class_RC_Data.Class_RC.Vista_MaterialesDeposito(conn)
                saldo.Where.CodMaterial.Value = codMaterial
                saldo.Where.CodSeccion.Value = codSeccion
                saldo.Where.CodDeposito.Value = codDeposito
                'saldo.Where.Saldo.Value = cantidad
                'saldo.Where.Saldo.Operator = WhereParameter.Operand.Greate
                If Not saldo.Query.Load Then
                    Throw New Exception("No posee cantidad necesaria del material en el deposito seleccionado")
                Else

                    If Integer.Parse(saldo.DefaultView.Table.Rows(0)("Saldo")) < cantidad Then

                        Throw New Exception("No posee cantidad necesaria del material en el deposito seleccionado")

                    End If

                End If

            End If

            detalle.AddNew()
            detalle.CodMovMaterial = codMovMaterial
            detalle.CodMaterial = codMaterial



            Dim pro As New Class_RC_Data.Class_RC.MATERIALES(conn)
            Dim detamaterial As New Class_RC_Data.Class_RC.DETALLEMATERIAL(conn)
            pro.Where.CodMaterial.Value = codMaterial
            detamaterial.Where.CodMaterial.Value = codMaterial
            If pro.Query.Load Then
                If detamaterial.Query.Load Then
                    If tipoMovimiento = "ENTRADA" Then
                        detalle.Cantidad = CDec(cantidad * detamaterial.CantUnidadMaterial)
                    ElseIf tipoMovimiento = "SALIDA" Then
                        detamaterial.CantExistente -= cantidad
                        pro.CantExistente -= (cantidad / detamaterial.CantUnidadMaterial)
                        detalle.Cantidad = CDec(cantidad)
                        detamaterial.Save()
                    End If
                    detalle.Save()
                    pro.Save()
                End If
            End If
            tx.CommitTransaction()

            Return True

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return False
        End Try

    End Function

    Public Function Delete_MovimientosMaterial_Detalle(conn As String, codDetalleMovMaterial As Integer) As Boolean

        Dim tx As TransactionMgr
        tx = TransactionMgr.ThreadTransactionMgr()

        Try
            tx.BeginTransaction()

            Dim detalle As New Class_RC_Data.Class_RC.DETALLEMOV_MATERIALES(conn)

            detalle.Where.CodMovMaterialDetalle.Value = codDetalleMovMaterial

            If detalle.Query.Load Then
                detalle.MarkAsDeleted()
                detalle.Save()
            End If

            tx.CommitTransaction()

            Return True

        Catch ex As Exception
            tx.RollbackTransaction()
            MessageError = ex
            Return False
        End Try

    End Function

    Public Function get_MovimientosMaterial_Detalles(conn As String, codMovMaterial As Integer) As DataTable
        Try

            Dim vDetalle As New Class_RC_Data.Class_RC.VistaMovMaterialesDetalle(conn)

            If codMovMaterial > 0 Then
                vDetalle.Where.CodMovMaterial.Value = codMovMaterial
            End If

            If vDetalle.Query.Load Then
                If vDetalle.DefaultView.Table.Rows.Count > 0 Then
                    Return vDetalle.DefaultView.Table
                Else
                    Return Nothing
                End If

            Else
                Return Nothing

            End If

        Catch ex As Exception
            MessageError = ex
            Return Nothing
        End Try

    End Function

    Public Function get_MovimientosMaterial_Imprimir(ByVal conn As String, ByVal codMov As Integer, ByVal fechaInicio As DateTime,
                                   ByVal fechaFin As DateTime, ByVal tipoMov As String, ByVal proveedor As String, ByVal cliente As String,
                                   ByVal nroPedido As Integer, ByVal nroFactura As String, ByVal rucPro As String, ByVal rucCliente As String) As DataTable
        Try

            Dim vMov As New Class_RC_Data.Class_RC.VistaMovMaterialesImprimir(conn)

            If codMov > 0 Then
                vMov.Where.CodMovMaterial.Value = codMov
            End If

            If nroPedido > 0 Then
                vMov.Where.NroNota.Value = nroPedido
            Else
                vMov.Where.FechaMov.BetweenBeginValue = New DateTime(fechaInicio.Year, fechaInicio.Month, fechaInicio.Day, 0, 0, 0)
                vMov.Where.FechaMov.BetweenEndValue = New DateTime(fechaFin.Year, fechaFin.Month, fechaFin.Day, 23, 59, 59)
                vMov.Where.FechaMov.Operator = WhereParameter.Operand.Between
            End If

            If tipoMov <> "" Then
                vMov.Where.MovimientoTipo.Value = tipoMov
            End If

            If proveedor <> "" Then
                vMov.Where.Proveedor.Value = "%" + proveedor + "%"
                vMov.Where.Proveedor.Operator = WhereParameter.Operand.Like
            End If

            If cliente <> "" Then
                vMov.Where.Cliente.Value = "%" + cliente + "%"
                vMov.Where.Cliente.Operator = WhereParameter.Operand.Like
            End If

            If rucPro <> "" Then
                vMov.Where.Ruc.Value = "%" + rucPro + "%"
                vMov.Where.Ruc.Operator = WhereParameter.Operand.Like
            End If

            If rucCliente <> "" Then
                vMov.Where.RucCliente.Value = "%" + rucCliente + "%"
                vMov.Where.RucCliente.Operator = WhereParameter.Operand.Like
            End If

            vMov.Query.AddOrderBy("fechaMov", WhereParameter.Dir.DESC)

            If vMov.Query.Load Then
                If vMov.DefaultView.Table.Rows.Count > 0 Then
                    Return vMov.DefaultView.Table
                Else
                    Return Nothing
                End If

            Else
                Return Nothing

            End If

        Catch ex As Exception
            MessageError = ex
            Return Nothing
        End Try

    End Function

    Public Function get_Materiales_Deposito(ByVal conn As String) As DataTable
        Try

            Dim vVentas As New Class_RC_Data.Class_RC.Vista_MaterialesDeposito(conn)
            If vVentas.Query.Load Then
                If vVentas.DefaultView.Table.Rows.Count > 0 Then
                    Return vVentas.DefaultView.Table
                Else
                    Return Nothing
                End If

            Else
                Return Nothing

            End If

        Catch ex As Exception
            MessageError = ex
            Return Nothing
        End Try

    End Function
#End Region

End Class
